<?php
/*
 * akimimi front
 *
 * OrderListMy View
 *
 * @author by M.Wang 
 *
 */

require_once '../app/views/BaseView.php';

class OrderListView extends BaseView {

    protected $selfModel = true;
    protected $formAction = '/userinfo/myorderlist/';
    public $orders;
    public $formdata;
    public $orderStatus;
    public $pager;
    public $isSuperAgent;
    public $userid;

    public function __construct($bool_selfModel=true){
        parent::__construct();
        $this->selfModel = $bool_selfModel;
        if( !$this->selfModel )
            $this->formAction = '/userinfo/schoolorderlist/';
    }

    public function getPage(){
        $arr_navsetting = array(
            array('url'=>'/userinfo/index', 'text'=>MYAKIMIMITXT),
            array('text'=>OLMYORDERTXT)
        );
        $this->setPageTitle(OLMYORDERTXT);
        $this->addCSS('myaccount.css');
        $this->addCSS('myaccountleftnav.css');
        $this->addCSS('jquery.ui.css');
        $this->addJS('lib/ui.core.js',true);
        $this->addJS('lib/jquery-ui-datepicker.js',true);
        $str_js = "
            <script type='text/javascript'>
            $(document).ready(function() {
                $('#order_createdd').datepicker({
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'yy-mm-dd'
                });
                    $('#order_createdu').datepicker({
                    changeMonth: true,
                    changeYear: true,
                    dateFormat: 'yy-mm-dd'
                });
            });
            var orderActionNextStep = function(int_orderid,int_status){
                $('#oaf_selfModel').val(" . ($this->selfModel ? '1' : '') . ");
                $('#oaf_order_id').val(int_orderid);
                $('#oaf_order_status').val(int_status);
                $('#orderActionForm').submit();
            };
            function openDialog(){
        	    var int_width = window.screen.availWidth;
        	    var int_height = window.screen.availHeight;
        	    $( 'body' ).css('overflow-x','hidden');
        	    $('#paymentDialog').css('width',int_width + 'px');
        	    $('#paymentDialog').css('height',window.document.body.clientHeight + 'px');
        	    $('#paymentDialogContent').css('margin-top', (int_height > 120 ? (int_height-120)*0.3 : 0) + 'px');
        	    $('#paymentDialogContent').css('margin-left', (int_width > 320 ? (int_width-320)*0.5 : 0) + 'px');
        	    $('#paymentDialog').css('display','block');
        	    window.scroll(0,0);
            }
            </script>";
        $this->setHeadExtends($str_js);
        $header = '';
        require_once '../app/views/MyAccoutLeftNavView.php';
        $myAccountLeftNavView = new MyAccountLeftNav();
        $str_result = $header
                    . $this->getNavigation($arr_navsetting)
                    . "<div id='mymimi_body'>"
                    . $myAccountLeftNavView->getMyAccountLeftNav()
                    . $this->getContent()
                    . "</div>";
        return $this->getHtmlPage($str_result);
    }

    public function getContent() {
    	$str_outer = "<form id='filterForm' method='post' action='{$this->formAction}'><div id='mymimi_right'><div class='right_bj'></div>%s</div></form>";
        $str_outer.= "<form id='orderActionForm' method='post' action='/userinfo/nextstep'>
                    <input type='hidden' id='oaf_selfModel' name='selfModel' />
                    <input type='hidden' id='oaf_order_id' name='order_id' />
                    <input type='hidden' id='oaf_order_status' name='order_status' />
                    </form>";
        $str_outer.= <<<str_outer
        <div id="paymentDialog" style="left:0px;top:0px;background-color:#000;display:none;position:absolute;z-index:9;text-align:center;font-size:12px;filter:alpha(opacity=80);-moz-opacity:0.8;" onclick='try{event.cancelBubble = true;}catch(e){event.stopPropagation();}'>
            <div id='paymentDialogContent' style="position:static;height:120px;width:320px;background-color:#fff;border:1px solid #000;font-size:16px;">
                <div style='background-color:#690;line-height:24px;height:24px;;font-size:1em;text-align:left;'>
                    <span style='margin-left:5px;font-weight:700;color:#fff;'>操作提示</span>
                </div>
                <div style="margin-top:10px;height:48px;font-size:12px;line-height:24px;">
                    <span>请您在新打开的页面上完成操作</span><br/>
                    <span>完成前请<b>不要关闭</b>此窗口</span>
                </div>
                <div style='height:48px;padding:5px;'>
                    <input style="cursor:pointer" type="button" value="已完成"  onclick="window.location.reload();"/>
                    <input style="cursor:pointer" type="button" value="我遇到问题了" onclick="window.location='/u/help?type=buyprocess';"/>
                </div>
            </div>
        </div>
str_outer;
        $str_search = $this->getSearchBlock();
    	$str_orderContent = $this->getOrderListBlock();
    	return sprintf($str_outer, $str_search . $str_orderContent);
    }

    public function getSearchBlock(){
        $arr_param = $this->formdata;
        $arr_statusSetting = $this->orderStatus;
        return "<div class='order_row'><ul>"
               . "<li style='width: 60px; text-align: center;'>" . OLORDERIDTXT . '</li>'
               . "<li style='width:170px;'><input class='input1' style='width:146px;' type='text' id='order_number' name='order_id' value='{$arr_param['order_id']}' /></li>"
               . "<li style='width: 60px; text-align: center;'>" . OLSPNAMELBL . '</li>'
               . "<li style='width:180px;'><input  class='input1' style='width:146px;' type='text' id='product_name' name='product_name' value='{$arr_param['product_name']}' /></li>"
               . "<li style='width: 60px; text-align: center;'>" . OLORDERSTATUSTXT . '</li>'
               . "<li style='width:170px;'><select id='order_status' name='order_status'>"
               . $this->getOrderStatus($arr_statusSetting,$arr_param['order_status'])
               . '</select></li></ul></div>'
               . "<div class='order_row'><ul>"
               . "<li style='width: 60px; text-align: center;'>" . OLSTIMEDLBL . '</li>'
               . "<li style='width:170px;'><input type='text' id='order_createdd' name='order_createdd' value='{$arr_param['order_createdd']}' />"
               . "<li style='width: 25px; text-align: center;'>" . OLSTIMEULBL . '</li>'
               . "<li style='width:170px;'><input type='text' id='order_createdu' name='order_createdu' value='{$arr_param['order_createdu']}' /></li>"
               . "<li><input type='submit' value=' " . OLSSUBMITBTN . " ' id='button' /></li></ul>"
               . "<input type='hidden' id='pageNumber' name='page' value='{$this->pager['nowpage']}' />"
               . "<input type='hidden' id='liveOrder' name='liveOrder' value='{$arr_param['liveOrder']}' />"
               . '</div>';
    }

    protected function getOrderStatus($arr_status, $int_select){
        $str_result = '';
        foreach ($arr_status as $int_key => $str_status){
            if ($int_key < 1){
                $str_result.= "<option value=''>" . OLSSTATUSNORMALOPT . "</option>";
                continue;
            }
            if ( $int_key > 20 )
               continue;
            if (!$this->selfModel && $int_key < 10)
                continue;
            $str_result.= "<option value='{$int_key}' "
                        . ( $int_key == $int_select ? "selected='selected'" : '' )
                        . ">{$str_status}</option>";
        }
        return $str_result;
    }

    public function getOrderListBlock(){
        $arr_param = $this->orders;
        $str_result = "<div id='order_list'>%s</div>";
        $str_selectModel = $this->getSelectModel($this->formdata['liveOrder']);
        $str_orders = $this->getOrders();
        $str_pager = $this->getPager();
        return sprintf($str_result, $str_selectModel.$str_orders.$str_pager);
    }

    public function getOrders(){
        $arr_orders = $this->orders;
        $str_outer = "<div class='order_border'><div>%s</div></div>";
        if (empty($arr_orders))
            return sprintf($str_outer,'');
        $str_content = '';
        foreach ($arr_orders as $arr_order) {
        	$str_content.= $this->getOrderListTableTitle($arr_order);
        	$str_content.= $this->getOrderListTableHead();
        	$str_content.= $this->getOrderListItems($arr_order['items']);
        	$str_content.= $this->getOrderItemsTotal($arr_order);
        	$str_content.= $this->getOrderFooter($arr_order);
        }
        return sprintf($str_outer,$str_content);
    }

    protected function getOrderFooter($arr_order) {
        $orderService = new OrderService();
    	return "<div class='order_row_x'><ul>"
    	       . "<li class='style_1'><a href='/userinfo/showOrder/{$arr_order['id']}'>" . OLORDERDETAILTXT . '</a></li>'
    	       . "<li class='style_2'>" . (($arr_order['status'] == '5' || $arr_order['status'] == '15')?OLORDERFINISHEDTXT . $arr_order['modified']:'') . '</li>'
    	       . "<li class='style_3'>" . OLORDERCOSTTYPETXT . Order::getOrderPaymentMethod( $arr_order['payment_method'] ) . '</li>'
    	       . "<li class='style_4'>" . OLORDERCONVEYANCETYPETXT . Order::getOrderShippingExpect( $arr_order['shipping_expect'] ) . '</li>'
    	       . "<li class='style_5'>" . ($this->beableToAction($this->userid, $arr_order)?$orderService->getOrderAction( $arr_order['status'], $arr_order['id'], $arr_order['payment_method'] ):'') . '</li>'
    	       . '</ul></div>';
    }

    protected function getOrderItemsTotal($arr_order) {
    	return "<div class='order_row_p'><ul>"
    	       . "<li class='style_1'></li>"
    	       . "<li class='style_2'>" . OLORDERITEMDISCOUNTTXT . MONEYTAG . $arr_order['discount_fee'] . '</li>'
    	       . "<li class='style_3'>" . OLORDERITEMFREIGHTTXT . MONEYTAG . $arr_order['shipping_fee'] . '</li>'
    	       . "<li class='style_4'>" . OLORDERITEMTOTALTXT . MONEYTAG . $arr_order['total'] . '</li>'
    	       . '</ul></div>';
    }

    protected function getOrderListItems($arr_items) {
    	$str_content = '';
    	foreach ($arr_items as $arr_item) {
    		$str_content.= "<div class='order_row_3'><ul>";
    		$str_content.= "<li class='style_1'>" . (empty($arr_item['title']) ? $arr_item['item_name'] : "<a href='/p/" . urlencode($arr_item['url_title']) . "'>{$arr_item['title']}</a>") . '</li>';
    		$str_content.= "<li class='style_2'>" . OLPRODUCTSELLERNAME . "</li>";
    		$str_content.= "<li class='style_3'>" . MONEYTAG . $arr_item['sale_price'] . '</li>';
    		$str_content.= "<li class='style_3'>" . $arr_item['quantity'] . '</li>';
    		$str_content.= "<li class='style_3'>" . ($arr_item['cheque_status'] ? ODCHEQUEKEEPEDTXT : ODCHEQUEUNKEEPTXT) . '</li>';
    		$str_content.= "<li class='style_3'>" . $this->getChequePoint(Order::getDefaultChequeRuleName($arr_item['cheque_rule'])) . '</li>';
    		$str_content.= "<li class='style_4'>" . MONEYTAG . number_format($arr_item['sale_price']*$arr_item['quantity']+$arr_item['tax'],2) . "</li>";
    		$str_content.= '</ul></div>';
    	}
    	return $str_content;
    }

    protected function getChequePoint($str_type){
        if (preg_match('/([\d]{1,2}%)/', $str_type, $arr_search))
            return $arr_search[1];
        return '0%';
    }

    protected function getOrderListTableHead(){
        return "<div class='order_row_2'><ul>"
               . "<li class='style_1'>" . OLORDERITEMTH0 . '</li>'
               . "<li class='style_2'>" . OLPRODUCTSELLERTXT . '</li>'
               . "<li class='style_3'>" . OLORDERITEMTH1 . '</li>'
               . "<li class='style_3'>" . OLORDERITEMTH2 . '</li>'
               . "<li class='style_3'>" . OLORDERITEMTH3 . '</li>'
               . "<li class='style_3'>" . OLORDERITEMTH4 . '</li>'
               . "<li class='style_4'>" . OLORDERITEMTH5 . '</li>'
               . '</ul></div>';
    }

    protected function getOrderListTableTitle($arr_order){
        return "<div class='order_row_1'>"
               . '<ul>'
               . "<li class='style_1'>" . OLORDERIDTXT . "<span>{$arr_order['id']}</span></li>"
               . "<li class='style_2'>" . OLORDERCUSTOMERTXT . $arr_order['user_name'] . '</li>'
               . "<li class='style_3'>" . OLORDERCREATEDTIMETXT . $arr_order['created'] . '</li>'
               . "<li class='style_4'>" . OLORDERSTATUSTXT . ( $arr_order['is_returned'] == '1' ? ORDERSSTATUSISRETURN : Order::getOrderStatusFront( $arr_order['status'] ) )
               . '</li></ul></div>';
    }

    protected function getPager(){
        $arr_param = $this->pager;
        $pager = new Pager('filterForm','pageNumber',true);
        return "<div class='p2_page'>" . $pager->paginate($arr_param['nowpage'],$arr_param['countrows'],$this->formAction,$arr_param['pageLimit']) . '</div>';
    }

    protected function getSelectModel($int_model) {
        return "<div id='order_nav'><ul>"
	           . "<li class='title_" . ($int_model==1?1:2) . "' onclick=\"window.location='{$this->formAction}';\">" . OLQS1 . '</li>'
	           . "<li class='title_" . ($int_model==2?1:2) . "' onclick=\"$('#liveOrder').val(2);$('#filterForm').submit();\">" . OLQS2 . '</li>'
	           . "<li class='title_" . ($int_model==3?1:2) . "' onclick=\"$('#liveOrder').val(3);$('#filterForm').submit();\">" . OLQS3 . '</li>'
	           . "<li class='title_" . ($int_model==4?1:2) . "' onclick=\"$('#liveOrder').val(4);$('#filterForm').submit();\">" . OLQS4 . '</li>'
	           . "<li class='title_" . ($int_model==5?1:2) . "' onclick=\"$('#liveOrder').val(5);$('#filterForm').submit();\">" . OLQS5 . '</li>'
	           . "</ul></div>";
    }

    protected function beableToAction($int_userid, $arr_order){
    	if( $arr_order['is_returned'] ) {
    		return false;
    	}
    	if( $arr_order['status'] > 0 && $arr_order['status'] < 10 ){
    		if( $int_userid != $arr_order['user_id'] )
    		  return false;
    		return true;
    	}else{
    	   if( $int_userid != $arr_order['agent_id'] )
    		  return false;
    	   return true;
    	}
    }

}
?>
